#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    int cnt = 5;
    while(cnt--)
    {
      printf("i am child process,cnt:%d pid:%d,ppid:%d\n",cnt,getpid(),getppid());
      sleep(1);
    }
    exit(1);
  }

  //parent
  while(1)
  {
    int status = 0;
    pid_t ret_id = waitpid(id,&status,WNOHANG);
    if(ret_id < 0)
    {
      printf("waitpid error!\n");
      exit(1);
    }
    else if(ret_id == 0)
    {
      printf("i am parent ,child no die i just do something else\n");
      sleep(1);
      continue;
    }
    else 
    {
      printf("i am parent , wait successfully,pid:%d,ppid:%d,ret_id:%d,child exit code:%d,child exit signal:%d\n"\
      ,getpid(),getppid(),ret_id,(status>>8)&0xFF,status & 0x7F);
      break;
    }
  }

  //printf("hello world");
  //_exit(-1);
  //
  //for(int i = 0;i<200;i++)
  //{
  //  printf("%d--->%s",i,strerror(i));
  //}
  //return 0;
}
